clear
cd "/Users/pedrovicente/Dropbox/Research Pedro/Data work - Angola/Randomization"

use surveyschool.dta, replace

set more off

set seed 20150107

*make 4-tuples based on municipality (first) and characteristics (second)
capture drop tuple
gen R1 = uniform()
rename S2_1_4_C1_Qa nclassrooms
gen nstudents=S4_1_C1_Ano1+S4_1_C1_Ano2+S4_1_C1_Ano3+S4_1_C1_Ano4+S4_1_C1_Ano5+S4_1_C1_Ano6+S4_1_C1_Ano7+S4_1_C1_Ano8+S4_1_C1_Ano9
factor nclassrooms nstudents
predict SCHOOLCHARACTER
encode MunicipioDescricao, g(MUNICIPALITY)
sort MUNICIPALITY SCHOOLCHARACTER R1
gen tuple = floor(_n/4 -.25)+1

keep MunicipioDescricao ComunaDescricao P1_Rec P12 P2_Rec MUNICIPALITY nclassrooms nstudents S9_1 S7_1 SCHOOLCHARACTER tuple

*randomization
capture drop Treatment
gen R2 = uniform()
by tuple, sort: egen Treatment = rank(R)
replace Treatment = Treatment -1
drop R2

label define pTreatment 0 "Control" 1 "Treatment 1" 2 "Treatment 2"  3 "Treatment 3"
label values Treatment pTreatment
list MunicipioDescricao ComunaDescricao P1_Rec P12 P2_Rec Treatment

*balance tests
gen noparentcom=0
replace noparentcom=1 if S9_1=="N�o"
global school="nclassrooms nstudents S7_1 noparentcom"
foreach i in $school {
	regress `i' i.Treatment
}

drop MUNICIPALITY nclassrooms nstudents S9_1 S7_1 SCHOOLCHARACTER noparentcom
sort Treatment P1_Rec
export excel "schoolsrandomsinfic.xlsx", firstrow(variables) replace
